﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using baseSet.importExport;

namespace baseSet.importExport
{
    public partial class FrmExcelExport : Form
    {
        string moduleName;
        DataGridView dgrid;
        string strTitle;
        itemListBLL item = new itemListBLL();


        public FrmExcelExport(string _moduleName, DataGridView _dgrid, string _strTitle)
        {
            InitializeComponent();
            moduleName = _moduleName;
            dgrid = _dgrid;
            strTitle = _strTitle;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

            try
            {
                saveFileDialog1.Filter = "EXCEL文件(*.XLS)|*.XLS";
                saveFileDialog1.ShowDialog();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void FrmExcelExport_Load(object sender, EventArgs e)
        {

            bSourceItemList.DataSource = item.getitemListBymoduleNum(moduleName); ;
            dataGridView1.DataSource = bSourceItemList;
            //bSourceItemList.Filter = "moduleName='" + moduleName + "'" + "";
            //baseSetBLL.setGridColumn(dataGridView1, "itemList");
           // BaseSetDLL.gridViewSet.setGridColumn(dataGridView1, "itemList");
            dataGridView1.AllowUserToAddRows = false;

            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                if (column.HeaderText == "itemDispC" || column.HeaderText == "itemDispNo" || column.HeaderText == "itemDispIndex" || column.HeaderText == "itemWidth")
                {
                    column.Visible = true;
                    if (column.HeaderText == "itemDispC")
                    {
                        column.HeaderText = "列名";
                    }

                    if (column.HeaderText == "itemDispNo")
                    {
                        column.HeaderText = "是否导出";
                    }

                    if (column.HeaderText == "itemDispIndex")
                    {
                        column.HeaderText = "顺序";
                        //column.ReadOnly = true;
                    }

                    if (column.HeaderText == "itemWidth")
                    {
                        column.HeaderText = "列宽";

                    }
                }
                else
                {
                    column.Visible = false;
                }

            }

            dataGridView1.Sort(dataGridView1.Columns["itemDispIndex"], ListSortDirection.Ascending);
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            try
            {

                sysDataSet.itemListDataTable exportTable = (sysDataSet.itemListDataTable)bSourceItemList.DataSource;
                //BaseSetDLL.baseDLL.ExportExcelTable(saveFileDialog1.FileName, dgrid, exportTable, moduleName, strTitle);
                //exportExcel.ExportExcelTable(saveFileDialog1.FileName, dgrid, exportTable, moduleName, strTitle);
                               // basicDataSetxsd.itemListDataTable exportTable = (basicDataSetxsd.itemListDataTable)bSourceItemList.DataSource;
                exportToExcel.SaveToFile(exportToExcel.RenderToExcel(dgrid, exportTable, strTitle, moduleName), saveFileDialog1.FileName);
                MessageBox.Show("导出成功，请去保存位置打开文件查看");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void buttonPevies_Click(object sender, EventArgs e)
        {
            try
            {
               
                DataGridViewCell currentCell = dataGridView1.CurrentRow.Cells["itemDispIndex"];
                int rowIndex = dataGridView1.CurrentRow.Index;
                if (rowIndex > 0)
                {
                    int dispIndex = Convert.ToInt32(currentCell.Value.ToString());
                    currentCell.Value = dispIndex - 1;
                    dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells["itemDispIndex"].Value = dispIndex;
                    dataGridView1.Sort(dataGridView1.Columns["itemDispIndex"], ListSortDirection.Ascending);

                    bSourceItemList.MovePrevious();
                }
            }
            catch { }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                DataGridViewCell currentCell = dataGridView1.CurrentRow.Cells["itemDispIndex"];
                int rowIndex = dataGridView1.CurrentRow.Index;
                if (rowIndex <dataGridView1.Rows.Count-1)
                {
                    int dispIndex = Convert.ToInt32(currentCell.Value.ToString());
                    currentCell.Value = dispIndex + 1;
                    dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells["itemDispIndex"].Value = dispIndex;
                    dataGridView1.Sort(dataGridView1.Columns["itemDispIndex"], ListSortDirection.Ascending);

                    bSourceItemList.MoveNext();
                }
            }
            catch { }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {

                item.itemListUpdateALL((sysDataSet.itemListDataTable)bSourceItemList.DataSource);
                
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //BaseSetDLL.gridViewSet.setGridViewRowNum(e, dataGridView1);
            DcolumnSet.setGridViewRowNum(e, dgrid);
        }


    }
}
